package com.flow.demo.module.gd.controller.app.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.flow.demo.framework.common.pojo.CommonResult;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.operatelog.core.annotations.OperateLog;
import com.flow.demo.framework.web.core.util.WebFrameworkUtils;
import com.flow.demo.module.gd.controller.app.user.dto.FullCondition;
import com.flow.demo.module.gd.controller.app.user.dto.GetUserInfoDto;
import com.flow.demo.module.gd.controller.app.user.dto.UploadImgDto;
import com.flow.demo.module.gd.controller.app.user.vo.*;
import com.flow.demo.module.gd.dal.dataobject.gduser.GdUserDO;
import com.flow.demo.module.gd.dal.mysql.gduser.GdUserMapper;
import com.flow.demo.module.gd.service.gduser.GdUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.flow.demo.framework.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;
import static com.flow.demo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.flow.demo.framework.common.pojo.CommonResult.success;
import static com.flow.demo.module.gd.enums.ErrorCodeConstants.GD_USER_NOT_EXISTS;

@Tag(name = "app - 用户")
@RestController
@Validated
public class AppUserController {


    @Resource
    private GdUserMapper gdUserMapper;

    @Resource
    private GdUserService gdUserService;

    @GetMapping("/getUserInfo")
    @Operation(summary = "获得用户信息")
    public CommonResult<GetUserInfoDto> getUserInfo() {
        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        GdUserDO gdUserDO = gdUserMapper.selectOne(GdUserDO::getId, loginUserId);
        if (gdUserDO == null)
            throw exception(GD_USER_NOT_EXISTS);
        return success( BeanUtil.toBean(gdUserDO,GetUserInfoDto.class));
    }


    @PostMapping("/updateUserInfo")
    @Operation(summary = "更新用户信息")
    public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody UpdateUserInfoVo updateReqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if(userId == null)
            throw exception(UNAUTHORIZED);
        updateReqVO.setId(userId.intValue());
        gdUserService.update(updateReqVO);
        return success(true);
    }


    @PostMapping("/sendCode")
    @Operation(summary = "发送验证码")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    @PermitAll
    public CommonResult<Boolean> sendCode(@Valid @RequestBody SendCodeVo vo) {
        gdUserService.sendCode(vo);
        return success(true);
    }


    @GetMapping("/verifyPhone")
    @Operation(summary = "验证手机号是否可以使用")
    public CommonResult<Boolean> verifyPhone(@Valid VerifyPhoneVo vo) {
        boolean flag= gdUserService.verifyPhone(vo);
        return success( flag);
    }


    @PostMapping("/verifyCode")
    @Operation(summary = "验证手机号验证码")
    public CommonResult<Boolean> verifyCode(@Valid  @RequestBody  BindPhoneVo vo) {
        boolean flag= gdUserService.verifyCode(vo);
        return success( flag);
    }


    @PostMapping("/bindPhone")
    @Operation(summary = "绑定手机号")
    public CommonResult<Boolean> bindPhone(@Valid @RequestBody BindPhoneVo vo) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if(userId == null)
            throw exception(UNAUTHORIZED);

        vo.setId(userId.intValue());
        gdUserService.bindPhone(vo);
        return success(true);
    }




    @PostMapping("/uploadImg")
    @Operation(summary = "上传图片")
    public CommonResult<UploadImgDto> uploadImg(MultipartFile file,@Valid  UploadImgVo vo) {
        UploadImgDto dto= gdUserService.uploadImg(file,vo);
        return success( dto);
    }



    @PostMapping("/verification")
    @Operation(summary = "验证是否有机会抽奖")
    public CommonResult<FullCondition> verificationHandler() {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if(userId == null)
            throw exception(UNAUTHORIZED);
        return success(gdUserService.getFullCondition(userId));
    }

}
